<?php
/**
 * Created by PhpStorm.
 * User: chniccs
 * Date: 2019-12-17
 * Time: 15:24
 */

namespace app\helper;


use app\constant\StoresConst;
use app\model\AdminStores;
use app\model\AdminTenant;
use app\model\AdminUser;

class InitHelper
{
    /**
     * 初始化检测
     * @param $userInfo
     * @return bool
     */
    public static function initCheck($userInfo){
        $userInfo = self::checkSite($userInfo);
        self::checkStore($userInfo['tid']);
        return $userInfo;
    }
    //检测是否已经创建了站点
    public static function checkSite($userInfo){
        if($userInfo['tid']===0){
            $tenant = AdminTenant::create(['uid'=>$userInfo['id'],'name'=>'我的站点','expiration_time' => date('Y-m-d H:i:s', strtotime('+15day')),]);//15天的试用期
            $userInfo['tid'] = $tenant->id;
            AdminUser::update(['tid'=>$tenant->id],['id'=>$userInfo['id']]);
        }
        return $userInfo;
    }
    //检测是否创建了自营店铺
    public static function checkStore($tid){
        $has = AdminStores::get(['tid'=>$tid,'level'=>StoresConst::LEVEL_SELF]);
        if(!$has){
            AdminStores::create(['tid'=>$tid,'level'=>StoresConst::LEVEL_SELF,'name'=>'官方店','expiration_time'=>'2050-01-01 00:00:00']);
        }
    }
}